package com.atguigu.gmall.auth.properites;

import com.atguigu.gmall.common.utils.RsaUtils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;

@Configuration
@ConfigurationProperties(prefix = "auth.jwt")
@Data
public class JwtProperties {

    //直接从application.yml配置文件赋值
    private String priKeyPath;
    private String pubKeyPath;
    private String keySecret;
    private int expireTime;
    private String cookieTokenName;
    private String cookieNickName;


    //@postConstruct后赋值
    private PrivateKey privateKey;
    private PublicKey publicKey;


    /**
     * 判断上述公钥私钥文件是否存在，没有生成一下
     */
    @PostConstruct
    public void checkKeyFile() throws Exception {
        //公钥文件不存在或者私钥文件不存在
        if (!(new File(priKeyPath).exists() && new File(pubKeyPath).exists())) {
            RsaUtils.generateKey(pubKeyPath, priKeyPath, keySecret);
        }
        privateKey = RsaUtils.getPrivateKey(priKeyPath);
        publicKey = RsaUtils.getPublicKey(pubKeyPath);
    }

}
